package com.njwb.day16.dom;

import com.njwb.day16.dom.util.DomUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class TestDemo01 {
    public static void main(String[] args) {
        Document document = DomUtil.getDocumentInstance("dahuaxiyou.xml");
        System.out.println("xml文件编码："+document.getXmlEncoding());
        //documentElement:books节点
        Element documentElement = document.getDocumentElement();
        //获取所有的book节点
        NodeList nodeList  = documentElement.getElementsByTagName("book");
        for(int i = 0 ;i<nodeList.getLength();i++) {
            //迭代出每一个book节点
            Node node =nodeList.item(i);
            for(Node currentNode=node.getFirstChild();currentNode!=null;currentNode=currentNode.getNextSibling()) {
                //把空白节点过滤掉 ==》只看元素节点
                if(currentNode.getNodeType()==Node.ELEMENT_NODE) {
                    System.out.println(currentNode.getFirstChild().getTextContent());
                }
            }
        }

    }





}
